/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  OwnerHireActivity.java   
 * @Package com.jiyi.activity.owner   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-10-11 下午4:59:34   
 * @version V1.0     
 */
package com.jiyi.activity.new_maintenance;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.jiyi.bean.maintenance.HireUnitBean;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.inteface.WebApi;
import com.jiyi.view.DateChooseDialog;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.CheckUtil;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: OwnerHireActivity
 * @Description:TODO(新增业主雇佣关系)
 * @author: HYM
 * @date: 2017-10-11 下午4:59:34
 * 
 */
public class AddHireActivity extends ActivityBase {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;
	
	@BindView(R.id.tvNameTip)
	TextView tvNameTip;

	@BindView(R.id.etOwnerName)
	EditText etOwnerName;
	@BindView(R.id.etFee)
	EditText etFee;
	@BindView(R.id.etStartDate)
	EditText etStartDate;
	@BindView(R.id.etEndDate)
	EditText etEndDate;

	final int CHOOSE = 1;
	String companyId;
	Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_hire);
		ButterKnife.bind(this);
		init();
	}

	@Override
	protected void onDestroy() {
		hideDialog(dialog);
		super.onDestroy();
	}

	private void init() {
		getIntentData();
		setTop();
		setViewState();
	}

	private void getIntentData() {
		if (UserInfoUtil.getCurrentUserRole() == AppConfig.PROJECT_MNG) {
			tvNameTip.setText("维保单位信息");
			tvSearch.setVisibility(View.GONE);
		}
	}

	private void setViewState() {
		etOwnerName.setKeyListener(null);
		etStartDate.setKeyListener(null);
		etEndDate.setKeyListener(null);
		// 设置必填项目验证
		etOwnerName.setTag(R.id.tag_view_empty, "业主名称不能为空");
		etFee.setTag(R.id.tag_view_empty, "金额不能为空");
		etStartDate.setTag(R.id.tag_view_empty, "合同开始时间不能为空");
		etEndDate.setTag(R.id.tag_view_empty, "合同结束时间不能为空");

	}

	private void setTop() {
		tvTitle.setText("新增雇佣关系");
		tvSearch.setText("已申请");
		tvSearch.setCompoundDrawables(null, null, null, null);
	}

	@OnClick({ R.id.tvBack, R.id.etOwnerName, R.id.etStartDate, R.id.etEndDate,
			R.id.tvSearch, R.id.tvPost })
	public void onClick(View view) {
		DateChooseDialog.Builder builder;
		Intent intent = null;
		switch (view.getId()) {
		case R.id.tvBack:
			finish();
			break;
		case R.id.tvSearch:
			intent = new Intent(this, HireRelativeActivity.class);
			startActivity(intent);
			break;
		case R.id.etOwnerName:
			intent = new Intent(this, NewHireRelativeActivity.class);
			startActivityForResult(intent, CHOOSE);
			break;
		case R.id.etStartDate:
			// 开始时间选择
			builder = new DateChooseDialog.Builder(this);
			builder.setTitle("选择日期");
			builder.setTextView((EditText) findViewById(R.id.etStartDate));
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							// 设置你的操作事项
						}
					});

			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			builder.create().show();
			break;

		case R.id.etEndDate:
			// 开始时间选择
			builder = new DateChooseDialog.Builder(this);
			builder.setTitle("选择日期");
			builder.setTextView((EditText) findViewById(R.id.etEndDate));
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							// 设置你的操作事项
						}
					});

			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			builder.create().show();
			break;
		case R.id.tvPost:
			// 申请绑定
			if (CheckUtil.checkView(etOwnerName, etFee, etStartDate, etEndDate)) {
				doApply();
			}
			break;
		default:
			break;
		}
	}

	void doApply() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());
		params.put("company_id", companyId);
		params.put("year_fee", etFee.getText().toString());
		params.put("start_time", etStartDate.getText().toString());
		params.put("end_time", etEndDate.getText().toString());

		String url ;
		if (UserInfoUtil.getCurrentUserRole() == AppConfig.PROJECT_MNG) {
			url = WebApi.OWNER_HIRE_APPLY;
		}
		else {
			url = WebApi.MAINTENANCE_HIRE_APPLY;
		}
		
		dialog = showDialog(dialog);
		VolleyRequest.StringRequestPost(this, url ,
				"", params, new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								ToastUtil.getInstances().showMsg(info);
								finish();
								if (UserInfoUtil.getCurrentUserRole() == AppConfig.PROJECT_MNG) {
									//业主申请用跳转到详情
									Intent intent = new Intent(AddHireActivity.this,HireDetailActivity.class);
									HireUnitBean hireUnitBean = new HireUnitBean();
									hireUnitBean.setWb_company_id(companyId);
									hireUnitBean.setWb_company_name(etOwnerName.getText().toString());
									hireUnitBean.setStart_time(etStartDate.getText().toString());
									hireUnitBean.setEnd_time(etEndDate.getText().toString());
									hireUnitBean.setStatus(0);
									hireUnitBean.setYear_fee(etFee.getText().toString());
									intent.putExtra("hire_unit", new Gson().toJson(hireUnitBean));
									startActivity(intent);
								}
								else {
									//维保公司申请后跳转到列表
									Intent intent = new Intent(AddHireActivity.this,HireRelativeActivity.class);
									startActivity(intent);
								}
								
							} else if (status.equals("404")
									&& info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg(
										"尚未登录或该用户在其他地方登录，请重新登录！");
								// 清除环信登录
								UserHelp.getInstance().clearUser(
										AppBase.Context, true);
								return;
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}

					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == CHOOSE && resultCode == RESULT_OK) {
			companyId = data.getStringExtra("companyId");
			String ownerName = data.getStringExtra("ownerName");
			etOwnerName.setText(ownerName);
		}
	}
}
